<?php

    include_once('app/connection.php');
    include_once('app_sv/check_user.php');
    
    $sql = mysql_query("SELECT * FROM hocky WHERE HOCKY=(SELECT MAX(HOCKY) FROM hocky WHERE NAM=(SELECT MAX(NAM) FROM hocky))
        AND NAM=(SELECT MAX(NAM) FROM hocky)");
    $row = mysql_fetch_assoc($sql);    
    $hocky = $row['HOCKY'];
    $nam = $row['NAM'];    
    
    if(isset($_POST['dk_mh']))
    {
        $mssv = $_SESSION['s_user'];
        
        //$sql = mysql_query("DELETE FROM dangky WHERE mssv=$mssv");
        
        $array = $_POST['dk_mh'];
        $count = count($array);
        for($i = 0; $i < $count; $i++) 
        {
            $v = explode(".", $array[$i]);
            
            $sql = mysql_query("INSERT INTO dangky (MSSV, MAMONHOC, MALOP, HOCKY, NAM) 
                VALUES ('$mssv', '$v[0]', '$v[1]', '$hocky', '$nam')");
        }
        
        $sql = mysql_query("SELECT DISTINCT CA,THU FROM dangky, lop WHERE lop.MALOP = dangky.MALOP AND lop.MAMONHOC = dangky.MAMONHOC
            AND lop.HOCKY = dangky.HOCKY AND lop.NAM = dangky.NAM AND dangky.NAM = (SELECT MAX(NAM) FROM dangky)
            AND dangky.HOCKY = (SELECT MAX(HOCKY) FROM dangky WHERE NAM = (SELECT MAX(NAM) FROM dangky))
            AND MSSV = '$mssv'");
        $count1 = mysql_num_rows($sql);
        
        $sql = mysql_query("SELECT CA,THU FROM dangky, lop WHERE lop.MALOP = dangky.MALOP AND lop.MAMONHOC = dangky.MAMONHOC
            AND lop.HOCKY = dangky.HOCKY AND lop.NAM = dangky.NAM AND dangky.NAM = (SELECT MAX(NAM) FROM dangky)
            AND dangky.HOCKY = (SELECT MAX(HOCKY) FROM dangky WHERE NAM = (SELECT MAX(NAM) FROM dangky))
            AND MSSV = '$mssv'");
        $count2 = mysql_num_rows($sql);
        
        if($count1 < $count2)
        {
            echo "<i>Bạn đã đăng ký trùng ca và thứ ở một số môn, vui lòng đăng ký lại</i>";
            $sql = mysql_query("DELETE FROM dangky WHERE MSSV = '$mssv' AND HOCKY = '$hocky' AND NAM = '$nam'");            
        }
            
        else echo "<script>window.location='?page=trangthai_dkmh';</script>";
    }
?>

<style>
    .danhsach tr:first-child{background: #247CB9;}
    .danhsach tr:nth-child(odd){background: #fff;}
    .danhsach tr:nth-child(odd){background: #ccc;}
</style>

<?php    
    if($is_sv)
    {
        echo "<center><h2> ĐĂNG KÝ MÔN HỌC </h2>";
        echo "<h3> HỌC KỲ ".$row['HOCKY']." NĂM ".$row['NAM']." </h3></center>";
        
?>
            
            <div style="width: 720px; min-height: 400px; overflow-y: scroll; margin: 0 auto;">
            <form name="dkmh" method="POST" action="">
            <center><table class="danhsach">
            
                <tr style="background: #247CB9;">
                    <td style="text-align: center; color: #fff;">Mã lớp</td>
                    <td style="text-align: center; color: #fff;">Tên Môn học</td>
                    <td style="text-align: center; color: #fff;">Tên Giảng viên</td>
                    <td style="width: 50px;text-align: center; color: #fff;">Ca</td>
                    <td style="text-align: center; color: #fff;">Thứ</td>                    
                    <td style="text-align: center; color: #fff;">Phòng</td>
                    <td style="text-align: center; color: #fff;">Tối đa</td>
                    <td style="text-align: center; color: #fff;">Đã ĐK</td>
                    <td style="text-align: center; color: #fff;">Chọn</td>
                </tr>
        <?php
                $sql = mysql_query("SELECT * FROM lop, giaovien, monhoc WHERE monhoc.MAMONHOC=lop.MAMONHOC 
                    AND lop.MAGIAOVIEN=giaovien.MAGIAOVIEN AND lop.HOCKY='$hocky' AND NAM='$nam'
                    ORDER BY lop.MAMONHOC, lop.MALOP");
                while($rows = mysql_fetch_assoc($sql))
                {
        ?>
                     <tr>
                     <td style="text-align: center;"><?php echo $rows['MAMONHOC'].'.'.$rows['MALOP']; ?></td>
                     <td><?php echo $rows['TENMONHOC']; ?></td>
                     <td><?php echo $rows['HOTEN']; ?></td>
                     <td style="text-align: center;"><?php echo $rows['CA']; ?></td>
                     <td style="text-align: center;"><?php echo $rows['THU']; ?></td>
                     <td style="text-align: center;"><?php echo $rows['PHONG']; ?></td>
                     <td style="text-align: center;"><?php echo $rows['TOIDA']; ?></td>
                     
                     <td style="text-align: center;">
                        <?php
                            $s = mysql_query("SELECT * FROM dangky WHERE MAMONHOC='".$rows['MAMONHOC']."' AND MALOP='".$rows['MALOP']."'");
                            $soluong = mysql_num_rows($s);
                            echo $soluong;
                        ?>
                     </td>
                     
                     <td style="text-align: center;">
                        <input type="checkbox" name="dk_mh[]" id="dk_mh" value="<?php 
                            echo $rows['MAMONHOC'].".".$rows['MALOP'].'" ';
                            if($soluong == $rows['TOIDA']) echo "onclick='return false' ";
                            
                            $s = mysql_query("SELECT * FROM dangky WHERE MSSV='".$_SESSION['s_user']."' 
                                AND MAMONHOC='".$rows['MAMONHOC']."' AND MALOP='".$rows['MALOP']."'
                                AND HOCKY='$hocky' AND NAM='$nam'");
                            if(mysql_num_rows($s) != 0) echo "checked";
                        ?> "/>
                     </td>
                     
                     </tr>
            <?php } ?>
            
            </table></center>            
            </div>
            
            <center><input type="submit" value="Đăng Ký" /></center>
            </form>
        
<?php
            
    }    
    else {
        echo "Bạn không có quyền xem! Vui lòng đăng nhập để có thể thực hiện chức năng này!";
    }
?>
